package org.tailor

class DesignController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [designInstanceList: Design.list(params), designInstanceTotal: Design.count()]
    }

    def create = {
        def designInstance = new Design()
        designInstance.properties = params
        return [designInstance: designInstance]
    }

    def save = {
        def designInstance = new Design(params)
        if (designInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'design.label', default: 'Design'), designInstance.id])}"
            redirect(action: "show", id: designInstance.id)
        }
        else {
            render(view: "create", model: [designInstance: designInstance])
        }
    }

    def show = {
        def designInstance = Design.get(params.id)
        if (!designInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'design.label', default: 'Design'), params.id])}"
            redirect(action: "list")
        }
        else {
            [designInstance: designInstance]
        }
    }

    def edit = {
        def designInstance = Design.get(params.id)
        if (!designInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'design.label', default: 'Design'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [designInstance: designInstance]
        }
    }
/* This is subtitute for image update
 
    def update = {
        def designInstance = Design.get(params.id)
        if (designInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (designInstance.version > version) {
                    
                    designInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'design.label', default: 'Design')] as Object[], "Another user has updated this Design while you were editing")
                    render(view: "edit", model: [designInstance: designInstance])
                    return
                }
            }
            designInstance.properties = params
            if (!designInstance.hasErrors() && designInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'design.label', default: 'Design'), designInstance.id])}"
                redirect(action: "show", id: designInstance.id)
            }
            else {
                render(view: "edit", model: [designInstance: designInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'design.label', default: 'Design'), params.id])}"
            redirect(action: "list")
        }
    }
*/
    def delete = {
        def designInstance = Design.get(params.id)
        if (designInstance) {
            try {
                designInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'design.label', default: 'Design'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'design.label', default: 'Design'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'design.label', default: 'Design'), params.id])}"
            redirect(action: "list")
        }
    }
    
    
    //picture
        def renderImage = {
	  def designInstance = Design.get(params.id)
		if (designInstance!=null) {
		   response.setContentLength(designInstance.designPicture.length)
		   byte[] image =designInstance.designPicture
		    response.outputStream.write(image)
	   }
	   else {
		   response.setContentType("image/jpeg")
		   response.getOutputStream().write("No Image".bytes)
	   }
   }
   
    def update = {
        def designInstance = Design.get(params.id)
		def designPicture = designInstance.designPicture
        if (designInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (designInstance.version > version) {
                    
                    designInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'design.label', default: 'Design')] as Object[], "Another user has updated this Design while you were editing")
                    render(view: "edit", model: [designInstance: designInstance])
                    return
                }
            }
            designInstance.properties = params
			if(params.designPicture.getSize() == 0){
				designInstance.designPicture = designPicture
				}
            if (!designInstance.hasErrors() && designInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'design.label', default: 'Design'), designInstance.id])}"
                redirect(action: "show", id: designInstance.id)
            }
            else {
                render(view: "edit", model: [designInstance: designInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'design.label', default: 'Design'), params.id])}"
            redirect(action: "list")
        }
    }

}
